package 数组.easy;

/**
 * @author Lw中
 * @date 2020/9/4 21:05
 */
public class SanShiWu {

    /**
     * 搜索插入位置
     * 方法：使用二分查找
     * @param nums
     * @param target
     * @return
     */

    public static int searchInsert(int[] nums, int target) {
        int low = 0, high = nums.length - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (target < nums[mid]) {
                high = mid - 1;
            } else if (target > nums[mid]) {
                low = mid + 1;
            } else {
                return mid;
            }
        }
        return low;
    }

    public static void main(String[] args) {
        int[] nums = {1,3,5,6};
        int i = searchInsert(nums, 2);
        System.out.println(i);
    }

}
